package com.easycarebn.main.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.SuggestNameType;
import com.easycarebn.request.data.DoctorDetail.Doctor;
import com.squareup.picasso.Picasso;
import com.timbacsi.main.R;

public class FragmentListDoctor extends Fragment {

	private View mView;
	private static FragmentListDoctor fragment = null;
	private ArrayList<Doctor> listdocter;
	private LinearLayout lineComment;

	public static FragmentListDoctor newInstance(String title) {
		if (fragment == null)
			fragment = new FragmentListDoctor();
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_list_doctor, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		lineComment = (LinearLayout) mView.findViewById(R.id.lineComment);
		lineComment.removeAllViews();

		listdocter = FragmentDrDetailt.newInstance("").getCurrentDoctor()
				.getListdoctor();
		if (listdocter == null || listdocter.size() == 0) {
			return;
		}

		addComment(listdocter);

	}

	private void addComment(ArrayList<Doctor> doctor) {
		if (doctor == null) {
			return;
		}
		for (final Doctor comment : doctor) {
			View viewItem = getActivity().getLayoutInflater().inflate(
					R.layout.item_doctor, null);
			ImageView imgAvatar = (ImageView) viewItem.findViewById(R.id.icon);
			Picasso.with(getActivity()).load(comment.getAvatar())
					.placeholder(R.drawable.avata).error(R.drawable.avata)
					.into(imgAvatar);

			TextView tvName = (TextView) viewItem.findViewById(R.id.name);
			TextView speciality = (TextView) viewItem
					.findViewById(R.id.speciality);
			TextView placeofwork = (TextView) viewItem
					.findViewById(R.id.placeofwork);

			tvName.setText(comment.getName());
			
			if (comment.getSpeciality()!= null
					&& comment.getSpeciality().size() > 0) {
				speciality.setText(comment.getSpeciality().get(0));
			}else{
				Utils.setTextEmpty(speciality, getActivity(), getResources().getString(R.string.lblChuyenkhoa));
			}
			
			if (comment.getPlaceofwork() != null
					&& comment.getPlaceofwork().size() > 0) {
				placeofwork.setText(comment.getPlaceofwork().get(0));
			}else{
				Utils.setTextEmpty(placeofwork, getActivity(), getResources().getString(R.string.lblChuyenkhoa));
			}
			viewItem.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Bundle data = new Bundle();
					data.putInt(Utils.KEY_DOCTOR_ID,
							Integer.parseInt(comment.getDoctor_id()));
					data.putInt(Utils.KEY_SERVICE_TYPE,
							SuggestNameType.DOCTOR.getCode());
					Utils.intentToDetailt(getActivity(),
							Utils.DOCTOR_SERVICE_DETAILT_PK, data);

				}
			});
			lineComment.addView(viewItem);
		}

	}

}
